<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd?'添加轮播图':'编辑轮播图'"
      :visible.sync="info.isShow"
      center
    >
      <el-form :model="bannerInfo" :rules="rules" ref="banner">
        <el-form-item prop="title" label="标题" :label-width="formLabelWidth">
          <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onpreview"
            :on-remove="onremove"
            :auto-upload="false"
            :on-change="onchange"
            :file-list='filelist'
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { banneradd, bannerinfo, banneredit } from "../../request/api.js";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      bannerInfo: {
        id: 1,
        title: "",
        img: "",
        status: 1
      },
      rules: {
        title: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ]
      },
      formLabelWidth: "120px",
      dialogVisible: false,
      dialogImageUrl: "",
      filelist: []
    };
  },
  props: ["info"],
  computed: {
    ...mapGetters({
      bannerlist: "banner/getList"
    })
  },
  methods: {
    ...mapActions({
      getBanner: "banner/getBannerActions"
    }),
    onchange(file, filelist) {
      console.log(file, 888);
      console.log(filelist, 999);
      this.bannerInfo.img = file.raw;
    },
    onpreview(file) {
      console.log(file, 333);
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    onremove() {},
    //回显
    lookup(id) {
      bannerinfo({ id }).then(res => {
        if (res.code == 200) {
          this.bannerInfo = res.list;
          this.bannerInfo.id = id;
          this.filelist = [{url:this.$http+this.bannerInfo.img}]
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.bannerInfo= {
        id: 1,
        title: "",
        img: "",
        status: 1
      };
        //调用清空验证的方法
      this.$refs.banner.resetFields();
      //清空图片文件
      this.filelist = []
    },
    confirm() {
      this.$refs.banner.validate(val => {
        if (val) {     
          if (this.info.isAdd) {     
            console.log(this.bannerInfo,99999);    
            banneradd(this.bannerInfo).then(res => {             
              if (res.code == 200) {        
                this.$message.success(res.msg);
                //重新调用
                this.getBanner();
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } 
          else {
            banneredit(this.bannerInfo).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    }
  }
};
</script>

<style  scoped>
</style>
